package com.example.wechat.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wechat.mapper.WeChatMapper;
import com.example.wechat.model.WechatMessage;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/messages")
public class MessageHistoryController {

    private final WeChatMapper messageRepository;

    public MessageHistoryController(WeChatMapper messageRepository) {
        this.messageRepository = messageRepository;
    }

    @GetMapping
    public IPage<WechatMessage> getMessages(
            @RequestParam(required = false) String otherUser,
            @RequestParam(required = false) String messageType,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            Principal principal) {
        
        // 参数校验
        if (page < 1) page = 1;
        if (size < 1 || size > 100) size = 20;
        
        Page<WechatMessage> pageParam = new Page<>(page, size);
        
        // 根据条件选择不同的查询方法
        if (otherUser != null && !otherUser.isEmpty()) {
            // 查询特定用户的聊天记录
            if (startTime != null && endTime != null) {
                // 有时间范围
                List<WechatMessage> messages = messageRepository.findByFromOrToAndTimestampBetween(
                    principal.getName(), 
                    otherUser, 
                    startTime.toString(), 
                    endTime.toString()
                );
                pageParam.setRecords(messages);
                pageParam.setTotal(messages.size());
                return pageParam;
            } else {
                // 无时间范围，使用分页查询
                return messageRepository.findMessagesWithPagination(
                    pageParam, 
                    principal.getName(), 
                    otherUser
                );
            }
        } else {
            // 查询当前用户的所有消息
            if (startTime != null && endTime != null) {
                // 有时间范围，先查询所有符合条件的记录
                List<WechatMessage> messages = messageRepository.findByFromOrToAndTimestampBetween(
                    principal.getName(), 
                    principal.getName(), 
                    startTime.toString(), 
                    endTime.toString()
                );
                // 手动分页
                int start = (page - 1) * size;
                int end = Math.min(start + size, messages.size());
                if (start < messages.size()) {
                    pageParam.setRecords(messages.subList(start, end));
                } else {
                    pageParam.setRecords(List.of());
                }
                pageParam.setTotal(messages.size());
                return pageParam;
            } else {
                // 无时间范围，使用分页查询
                return messageRepository.findPageByFrom(
                    pageParam, 
                    principal.getName()
                );
            }
        }
    }
}